//自定义分页插件
;(function($){  
    $.fn.degrid=function(params){  
        var gridbox = $(this);
        var rowsbox = "<div class='span12 grid_rows'></div>";
        var pagebox = "<div class='span12 grid_page'>" +
	        				"<div class='pagination text-center'>"+
								"<ul class='grid_pagebox'>"+
									
								"</ul>"+
							"</div>" +
					   "</div>";
    	gridbox.html(rowsbox+pagebox);
    	params.rowsbox = gridbox.find(".grid_rows");
    	params.pagebox = gridbox.find(".grid_page");
    	params.pageul = gridbox.find(".grid_page").find(".grid_pagebox");
    	params.gridbox = gridbox;
    	loadPage(params);
    }
    
    function loadPage(params){
		$.ajax({
			url: params.url,
			data: {
				page : params.page,
				rows : params.rows
			},
			dataType : "json",
			success: function(data){
				var hs = params.listRender(data.rows);
				params.rowsbox.html(hs);
				if(params.debind){
					params.debind();
				}
				params.totalPage =  data.totalPage;
				if(null != params.rows && undefined != params.rows && "" != params.rows && "undefined" != params.rows){
					renderPage(params);
				}
			}
		});
	}
    function renderPage(params){
		var pageNums = getPageNums(params);
		params.pageul.html(pageNums);
		params.pageul.find(".pagination_last").unbind().bind("click",function(){
			lastPage(params);
		});
		params.pageul.find(".pagination_next").unbind().bind("click",function(){
			nextPage(params);
		});
		params.pageul.find(".topagebtn").unbind().bind("click",function(){
			var pagenum = $(this).data("pagenum");
			params.page = pagenum;
			runPage(params);
		});
	}
	function getPageNums(params){
		var pageNums = "";
		var totalPage = params.totalPage;
		var page = params.page;
		if(totalPage > 1){
			if(page == 1){
				pageNums +="<li><a class='pagination_last' style='color:grey' href='javascript:void(0);'><<</a></li>";
			}else{
				pageNums +="<li><a class='pagination_last' href='javascript:void(0);'><<</a></li>";
			}
			
			if(totalPage <= 8){
				for(var i = 1; i <=totalPage; i++){
					if(i == page){
						pageNums += "<li><a href='javascript:void(0);' style='color:red' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
					}else{
						pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
					}
				}
			}else{
				if(page< (totalPage-4)){
					if(page>4){
						pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+(page-3)+"'>"+(page-3)+"</a></li>";
						pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+(page-2)+"'>"+(page-2)+"</a></li>";
						pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+(page-1)+"'>"+(page-1)+"</a></li>";
						pageNums += "<li><a href='javascript:void(0);' style='color:red' class='topagebtn' data-pagenum='"+page+"'>"+page+"</a></li>";
						pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+(page+1)+"'>"+(page+1)+"</a></li>";
					}else{
						for(var i = 1; i <= 5; i++){
							if(i == page){
								pageNums += "<li><a href='javascript:void(0);' style='color:red' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
							}else{
								pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
							}
						}
					}
					
					pageNums += "<li><a href='javascript:void(0);'>...</a></li>";
					pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+(totalPage-1)+"'>"+(totalPage-1)+"</a></li>";
					pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+totalPage+"'>"+totalPage+"</a></li>";
				}else{
					for(var i = totalPage-7; i <= totalPage; i++){
						if(i == page){
							pageNums += "<li><a href='javascript:void(0);' style='color:red' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
						}else{
							pageNums += "<li><a href='javascript:void(0);' class='topagebtn' data-pagenum='"+i+"'>"+i+"</a></li>";
						}
					}
				}
			}
			if(page == totalPage){
				pageNums += "<li><a class='pagination_next' style='color:grey' href='javascript:void(0);' >>></a></li>";
			}else{
				pageNums += "<li><a class='pagination_next' href='javascript:void(0);' >>></a></li>";
			}
			
		}else{
			pageNums += "<li><a href='javascript:void(0);' style='color:red' class='topagebtn' data-pagenum='1'>1</a></li>";
		}
		
		return pageNums;
	}
	
	function lastPage(params){
		if(params.page != 1){
			params.page --;
			loadPage(params);
		}
	}
	
	function nextPage(params){
		if(params.page != params.totalPage){
			params.page ++;
			loadPage(params);
		}
	}
	
	function runPage(params){
		loadPage(params);
	}
})(jQuery); 